/** I/O Application **/
/* MPPA headers */
#include <mppa/osconfig.h>
#include <mppaipc.h>

// number of clusters to use
#define CLUSTER_COUNT 16




int main (int argc, char** argv)
{
	int i;
	long long dummy = 0;
        long long match = -(1LL << CLUSTER_COUNT);		// matching value (e.g. 11111..1) to unblock the sync.
        const char *global_sync = "/mppa/sync/128:1";		// sync. connector address. 
								// 	set an ID (128 is the IODDR0, 196 for IODDR1) and a port
	const char *results_portal = "/mppa/portal/128:2"; 	// result portal (n cluster write on it)
        long int results[CLUSTER_COUNT];

	// open the sync.(Rx process) and arm the sync with the match value
	int global_sync_fd = mppa_open(global_sync, O_RDONLY);
        mppa_ioctl(global_sync_fd, MPPA_RX_SET_MATCH, match);

	// open the portal (Rx process)
	int results_portal_fd = mppa_open(results_portal, O_RDONLY);
	// portal initialization (binding portal and buffer results)
        mppa_aiocb_t results_portal_aiocb[1] = { MPPA_AIOCB_INITIALIZER(results_portal_fd, results, sizeof(results[0])*CLUSTER_COUNT) };
	// set trigger value used by mppa_aio_read() (CLUSTER_COUNT notifications)
        mppa_aiocb_set_trigger(results_portal_aiocb, CLUSTER_COUNT);
	// complete initialization: set binding and set counter 
	mppa_aio_read(results_portal_aiocb);

	// prepare the arguments for the cluster application (connector addresses and cluster id)
	char arg0[50];
        const char *argv_cluster[] = { arg0, global_sync, results_portal, 0 };
	for(i = 0; i < CLUSTER_COUNT; i++)
	{
		// spawn the cluster applications
		// 	- i = cluster id
		//	- "cluster" = binary to run on compute clusters
		//	- argv_clusters = arguments to pass the cluster application
		sprintf(arg0, "%d", i);
		mppa_spawn(i, NULL, "cluster", argv_cluster, 0);
	}	
	
	// I/O is blocked until clusters are ready (dummy = match)
	mppa_read(global_sync_fd, &dummy, sizeof(dummy));
	
	// wait until all clusters send the data (they send back the id)
	mppa_aio_wait(results_portal_aiocb);
	
	
	// print the results
	for (i=0; i < CLUSTER_COUNT; i++)
		printf("Hello world from cluster %d\n",results[i]);
	
}
